﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace InventoryManager
{
    public partial class ViewOrderForm_2_UserControl : UserControl
    {
        public event FormChangedHandler FormChangedEvent;

        private string _connectionString;
        private MySqlConnection _connection;
        private MySqlDataAdapter _mySqlDataAdapter;
        
        private DataTable _dataTable;

        private int _selectedRowIndex;
        private int _selectedColumnIndex;
        private string _orderID;
        private string _orderDate;

        public ViewOrderForm_2_UserControl(string connectionString, object[] param, int width, int height)
        {
            InitializeComponent();

            _connectionString = connectionString;
            _connection = new MySqlConnection(_connectionString);

            this.Width = width;
            this.Height = height;

            _selectedRowIndex = -1;
            _selectedColumnIndex = -1;
            _orderID = "";
            _orderDate = "";

            _dataTable = (DataTable)param[0];
            SearchResultDataGridView.DataSource = _dataTable;
            TitleLabel.Text = "ผลการค้นหา จากวันที่ " + (string)param[1] + " ถึงวันที่ " + (string)param[2];
        }

        private void OpenReport(int reportType, int orderID)
        {
            ShowPriceForm showPriceForm = new ShowPriceForm();
            ShowReportForm showReportForm;

            switch (showPriceForm.ShowDialog())
            {
                case DialogResult.OK:
                    showReportForm = new ShowReportForm(_connectionString);
                    showReportForm.setReportToShow(reportType, orderID, 
                        !showPriceForm.ShowPriceCheckBox.Checked, !showPriceForm.ShowWeightCheckBox.Checked, null);
                    showReportForm.LoadReport();
                    showReportForm.ShowDialog();
                    break;
                default:
                    break;
            }
        }

        private void UpdateSellerInfo(int orderID, string orderDate)
        {
            try
            {
                _connection.Open();

                string query = "SELECT SellerOrderID, SellerDate FROM orderidsellername WHERE OrderID = " + orderID;
                _mySqlDataAdapter = new MySqlDataAdapter(query, _connection);

                DataTable dataTable = new DataTable();
                _mySqlDataAdapter.Fill(dataTable);

                InsertSellerInfoForm insertSellerInfoForm =
                    new InsertSellerInfoForm(orderID.ToString("D6"), orderDate,
                        dataTable.Rows[0][0].ToString(), dataTable.Rows[0][1].ToString());

                switch (insertSellerInfoForm.ShowDialog())
                {
                    case DialogResult.OK:
                        MySqlCommand updateCmd = _connection.CreateCommand();
                        updateCmd.Parameters.AddWithValue("SellerOrderID", insertSellerInfoForm.SellerOrderID);
                        updateCmd.Parameters.AddWithValue("SellerDate", insertSellerInfoForm.SellerDate);
                        updateCmd.CommandText = "UPDATE orderidsellername SET SellerOrderID = ?SellerOrderID, " +
                            "SellerDate = ?SellerDate WHERE OrderID = " + orderID;
                        updateCmd.ExecuteNonQuery();

                        _dataTable.Rows[_selectedRowIndex][1] = insertSellerInfoForm.SellerOrderID;
                        _dataTable.Rows[_selectedRowIndex][3] = ThaiWordsGenerator.ToThaiDate(insertSellerInfoForm.SellerDate);
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                _connection.Close();
            }
        }

        private void SearchResultDataGridView_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            _selectedRowIndex = e.RowIndex;
            _selectedColumnIndex = e.ColumnIndex;

            if (_selectedRowIndex != -1 && _selectedColumnIndex != -1)
            {
                DataGridViewCell clickedCell = ((DataGridView)sender).Rows[_selectedRowIndex].Cells[0];

                ShowReportForm showReportForm = new ShowReportForm(_connectionString);
                string orderID = clickedCell.Value.ToString();
                OpenReport(1, Int32.Parse(orderID));
            }
        }

        private void BackButton_Click(object sender, EventArgs e)
        {
            FormChangedEvent("ORDERFORM_VIEW_1", null);
        }

        private void SearchResultDataGridView_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            _selectedRowIndex = e.RowIndex;
            _selectedColumnIndex = e.ColumnIndex;

            if (_selectedRowIndex != -1 && _selectedColumnIndex != -1)
            {
                if (e.Button == MouseButtons.Right)
                {
                    DataGridViewCell clickedCell = ((DataGridView)sender).Rows[_selectedRowIndex].Cells[_selectedColumnIndex];

                    // Here you can do whatever you want with the cell
                    this.SearchResultDataGridView.CurrentCell = clickedCell;  // Select the clicked cell, for instance

                    // Get mouse position relative to the vehicles grid
                    var relativeMousePosition = SearchResultDataGridView.PointToClient(Cursor.Position);

                    // Show the context menu and set ID
                    DataGridViewCell pickedCell = ((DataGridView)sender).Rows[_selectedRowIndex].Cells[0];
                    _orderID = pickedCell.Value.ToString();
                    pickedCell = ((DataGridView)sender).Rows[_selectedRowIndex].Cells[2];
                    _orderDate = pickedCell.Value.ToString();
                    RightClickMenu.Show(SearchResultDataGridView, relativeMousePosition);
                }
            }
        }

        private void OpenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenReport(1, Int32.Parse(_orderID));
        }

        private void UpdateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //UpdateSellerInfo(Int32.Parse(_orderID), _orderDate);
            EditOrderForm editOrderForm = new EditOrderForm(_connectionString, 1, Int32.Parse(_orderID));
            editOrderForm.EditFormClosedEvent += new EditFormClosedHandler(editOrderForm_EditFormClosedEvent);

            if (editOrderForm.ShowDialog() != DialogResult.OK)
            {
                _dataTable.Rows[_selectedRowIndex][1] = editOrderForm.SellerOrderIDTextBox.Text;
                _dataTable.Rows[_selectedRowIndex][3] = ThaiWordsGenerator.ToThaiDate(editOrderForm.SellerDateTimePicker.Value);
            }
        }

        void editOrderForm_EditFormClosedEvent(int recordType)
        {
            if (recordType == 1)
                FormChangedEvent("ORDERFORM_SELECTION", null);
        }
    }
}
